<?php

/////////////
function listdirs($name,$cacheoverride=0){
	global $dirsread,$globalcache,$dirsreadcached;
	//print "listdirs:$name<br>";
	
	if($globalcache["$name"]&&$cacheoverride!==1){
		$array=$globalcache["$name"];
		$dirsreadcached++;
	}else{
		
		@chmod($name,octdec(777));
		if(is_dir($name)){
			$handle=opendir($name);
			while ($file = readdir($handle)) {
				if(is_dir($name."/".$file)&&$file!=="."&&$file!==".."){
					$array[]=$file;
				}
			}
			closedir($handle);
		}
		
		$globalcache["$name"]=$array;
		$dirsread++;
		
	}
	
	return $array;
}

/////////////
function listfiles($name,$cacheoverride=0){
	global $dirsread,$globalcache,$dirsreadcached;
	//print "listfiles:$name<br>";
	
	if($globalcache["$name"]&&$cacheoverride!==1){
		$array=$globalcache["$name"];
		$dirsreadcached++;
	}else{
		
		@chmod($name,octdec(777));
		if(is_dir($name)){
			$handle=opendir($name);
			while ($file = readdir($handle)) {
				if(is_file($name."/".$file)){
					$array[]=substr($file,0,strrpos($file,"."));
				}
			}
			closedir($handle);
		}
		
		$globalcache["$name"]=$array;
		$dirsread++;
		
	}
	
	return $array;
}

/////////////
function listfilesext($name,$cacheoverride=0){
	global $dirsread,$globalcache,$dirsreadcached;
	
	if($globalcache["$name"]&&$cacheoverride!==1){
		$array=$globalcache["$name"];
		$dirsreadcached++;
	}else{
		
		@chmod($name,octdec(777));
		if(is_dir($name)){
			$handle=opendir($name);
			while ($file = readdir($handle)) {
				if(is_file($name."/".$file)){
					$array[]=$file;
				}
			}
			closedir($handle);
		}
		
		$globalcache["$name"]=$array;
		$dirsread++;
	}
	
	return $array;
}


////////////
function getdata($name,$cacheoverride=0){
	global $filesopened,$filesopenedcached,$globalcache;
	//echo $name."<br>";
	unset($dataarray2);
	
	@chmod($name,octdec(777));
	if(is_file($name)){
		if(filesize($name)>17){
			if(count($globalcache["$name"])>0&&$cacheoverride!==1){
				$dataarray2=$globalcache["$name"];
				$filesopenedcached+=1;
			}else{
				$dataarray=file($name);
			
				for($n=1;$n<count($dataarray);$n++){
					$dataarray2[$n-1]=trim($dataarray[$n]);
				}
				$globalcache["$name"]=$dataarray2;
				$filesopened++;
			}
		}
	}
	
	return $dataarray2;
}

////////////
function writedata($name,$data,$line){
	global $filewrites,$globalcache;
	
	$dataarray=getdata($name,1);
	
	@chmod($name,octdec(777));
	$open=fopen($name,"w");
	
	if($open){
		fwrite($open,"<?php die(); ?>\n");
		$filewrites++;
		
		for($n=0;$n<$line;$n++){
			fwrite($open,"$dataarray[$n]\n");
			$filewrites++;
		}
		
		fwrite($open,"$data\n");
		$filewrites++;
		
		for($n=$line+1;$n<count($dataarray);$n++){
			fwrite($open,"$dataarray[$n]\n");
			$filewrites++;
		}
		
		fclose($open);
	}
	
	$globalcache["$name"]=array();
	
	
}

/////////////
function writearray($name,$array,$line){
	
	global $filewrites,$globalcache;
	
	$dataarray=getdata($name,1);
	
	@chmod($name,octdec(777));
	$open=fopen($name,"w");
	
	if($open){
		fwrite($open,"<?php die(); ?>\n");
		$filewrites++;
		
		for($n=0;$n<$line;$n++){
			fwrite($open,$dataarray[$n]."\n");
			$filewrites++;
		}
		
		for($n=0;$n<count($array);$n++){
			fwrite($open,$array[$n]."\n");
			$filewrites++;
		}
		
		for($n=$line+count($array);$n<count($dataarray);$n++){
			fwrite($open,$dataarray[$n]."\n");
			$filewrites++;
		}
		
		fclose($open);
	}
	
	$globalcache["$name"]=array();
	
	
}


/////////////
function deletedata($name,$line){
	global $filewrites;
	
	$dataarray=getdata($name);
	@chmod($name,octdec(777));
	$open=@fopen($name,"w");
	
	if($open){
		fwrite($open,"<?php die(); ?>\n");
		$filewrites++;
		
		for($n=0;$n<$line;$n++){
			fwrite($open,"$dataarray[$n]\n");
			$filewrites++;
		}
		for($n=$line+1;$n<count($dataarray);$n++){
			fwrite($open,"$dataarray[$n]\n");
			$filewrites++;
		}
		
		fclose($open);
	}
	
	$globalcache["$name"]=array();
	
	
}

function createdir($name){
	if(!is_dir($name)){
		@mkdir($name,octdec(777));
		@chmod($name,octdec(777));
	}
}

function copydir($olddir,$newdir){

	@chmod($olddir,octdec(777));
	@chmod($newdir,octdec(777));

	createdir($newdir);
	
	$dirsarray=listdirs($olddir,1);
	for($n=0;$n<count($dirsarray);$n++){
		copydir("$olddir/$dirsarray[$n]","$newdir/$dirsarray[$n]");
	}
	
	$filesarray=listfilesext($olddir,1);
	for($m=0;$m<count($filesarray);$m++){
		copy("$olddir/$filesarray[$m]","$newdir/$filesarray[$m]");
	}
	
}

//deletes all subdirectories and files recursively

function deletedir($dir,$subdirs=1){

	@chmod($dir,octdec(777));
	
	$dirsarray=listdirs($dir,1);
	for($n=0;$n<count($dirsarray);$n++){
		deletedir("$dir/$dirsarray[$n]");
	}
	
	$filesarray=listfilesext($dir,1);
	for($m=0;$m<count($filesarray);$m++){
		unlink("$dir/$filesarray[$m]");
	}
	
	rmdir($dir);
}


?>
